<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="vue2," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="背景
vue2中父子组件的事件处理相对简单，参照官网的方式即可。但有时候我们也需要利用非父子组件的事件传递来做到模块和组件解耦，例如中后台管理系统中的导航菜单选择引起面包屑组件的变化，其他类似的场景各位同学可能也会遇到，下面我们根据官方的文档一起寻找解决方案。
备注: 如果需要共享的数据比较多的话，我们应该优先考虑使用vuex来管理应用数据，但vuex的使用并非足够简单。因此，如果应用相对简单时，">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2组件通信之事件传递">
<meta property="og:url" content="https://vector-ding.coding.me/技术/前端/vue2/Vue2组件通信之事件传递/index.html">
<meta property="og:site_name" content="Vector's Notes">
<meta property="og:description" content="背景
vue2中父子组件的事件处理相对简单，参照官网的方式即可。但有时候我们也需要利用非父子组件的事件传递来做到模块和组件解耦，例如中后台管理系统中的导航菜单选择引起面包屑组件的变化，其他类似的场景各位同学可能也会遇到，下面我们根据官方的文档一起寻找解决方案。
备注: 如果需要共享的数据比较多的话，我们应该优先考虑使用vuex来管理应用数据，但vuex的使用并非足够简单。因此，如果应用相对简单时，">
<meta property="og:updated_time" content="2017-03-08T07:20:52.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue2组件通信之事件传递">
<meta name="twitter:description" content="背景
vue2中父子组件的事件处理相对简单，参照官网的方式即可。但有时候我们也需要利用非父子组件的事件传递来做到模块和组件解耦，例如中后台管理系统中的导航菜单选择引起面包屑组件的变化，其他类似的场景各位同学可能也会遇到，下面我们根据官方的文档一起寻找解决方案。
备注: 如果需要共享的数据比较多的话，我们应该优先考虑使用vuex来管理应用数据，但vuex的使用并非足够简单。因此，如果应用相对简单时，">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6378305291301160000',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://vector-ding.coding.me/技术/前端/vue2/Vue2组件通信之事件传递/"/>





  <title> Vue2组件通信之事件传递 | Vector's Notes </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Vector's Notes</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle"></p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-algorithm">
          <a href="/categories/algorithm/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-superscript"></i> <br />
            
            Algorithm
          </a>
        </li>
      
        
        <li class="menu-item menu-item-java">
          <a href="/categories/java/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />
            
            Java
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="https://vector-ding.coding.me/技术/前端/vue2/Vue2组件通信之事件传递/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="vector.ding">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/assets/img/vector.ding.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Vector's Notes">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Vector's Notes" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Vue2组件通信之事件传递
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-27T10:03:28+08:00">
                2017-02-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/技术/" itemprop="url" rel="index">
                    <span itemprop="name">技术</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/技术/前端/" itemprop="url" rel="index">
                    <span itemprop="name">前端</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/技术/前端/vue2/" itemprop="url" rel="index">
                    <span itemprop="name">vue2</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/技术/前端/vue2/Vue2组件通信之事件传递/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="技术/前端/vue2/Vue2组件通信之事件传递/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><blockquote>
<p>vue2中父子组件的事件处理相对简单，参照官网的方式即可。但有时候我们也需要利用非父子组件的事件传递来做到模块和组件解耦，例如中后台管理系统中的导航菜单选择引起面包屑组件的变化，其他类似的场景各位同学可能也会遇到，下面我们根据官方的文档一起寻找解决方案。</p>
<p>备注: 如果需要共享的数据比较多的话，我们应该优先考虑使用<code>vuex</code>来管理应用数据，但<code>vuex</code>的使用并非足够简单。因此，如果应用相对简单时，可以考虑使用事件来做到组件通信（本质上是数据的共享）。</p>
</blockquote>
<p>首先看下参考官方给出的列子，<a href="https://cn.vuejs.org/v2/guide/components.html#非父子组件通信" target="_blank" rel="external">Here</a></p>
<a id="more"></a>
<blockquote>
<p>有时候非父子关系的组件也需要通信。在简单的场景下，使用一个空的 Vue 实例作为中央事件总线：
   <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> bus = <span class="keyword">new</span> Vue()</div><div class="line"><span class="comment">// 触发组件 A 中的事件</span></div><div class="line">bus.$emit(<span class="string">'id-selected'</span>, <span class="number">1</span>)</div><div class="line"><span class="comment">// 在组件 B 创建的钩子中监听事件</span></div><div class="line">bus.$on(<span class="string">'id-selected'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">id</span>) </span>&#123;</div><div class="line"><span class="comment">// ...</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>在更多复杂的情况下，你应该考虑使用专门的 <a href="https://cn.vuejs.org/v2/guide/state-management.html" target="_blank" rel="external">状态管理模式</a> 和 <a href="https://vuex.vuejs.org/zh-cn/" target="_blank" rel="external">vuex</a>.</p>
</blockquote>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><p>上面只是给出了用法，具体到项目中，有下面几种做法:</p>
<h3 id="1-将事件控制器抽离出来，作为单独的模块使用"><a href="#1-将事件控制器抽离出来，作为单独的模块使用" class="headerlink" title="1. 将事件控制器抽离出来，作为单独的模块使用"></a>1. 将事件控制器抽离出来，作为单独的模块使用</h3><blockquote>
<p>新建<code>EventBus</code>模块（或者采用Util模式，封装为方法），使用空的<code>vue</code>对象作为时间派发器，在需要发送和捕获的组建中，分别导入组件，使用<code>$emit</code>发送事件，<code>$on</code>捕获事件。</p>
</blockquote>
<ol>
<li><p>新建<code>EventBus.js</code></p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> EventBus = <span class="keyword">new</span> Vue()</div></pre></td></tr></table></figure>
</li>
<li><p>事件发送</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123; EventBus &#125; <span class="keyword">from</span> <span class="string">'src/EventBus.js'</span></div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    created() &#123;</div><div class="line">        ...</div><div class="line">    &#125;,</div><div class="line">    data() &#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            ...</div><div class="line">        &#125;</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">methods</span>: &#123;</div><div class="line">        onSelect(item) &#123;</div><div class="line">            <span class="comment">// 发送菜单选中事件</span></div><div class="line">            EventBus.$emit(<span class="string">'menu-selected'</span>, item)</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>事件捕获</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123; EventBus &#125; <span class="keyword">from</span> <span class="string">'src/EventBus.js'</span></div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    created () &#123;</div><div class="line">        <span class="comment">// 监听事件菜单选中事件</span></div><div class="line">        EventBus.$on(<span class="string">'menu-selected'</span>, item =&gt; &#123;</div><div class="line">            <span class="built_in">console</span>.log(item)</div><div class="line">            ...</div><div class="line">        &#125;)</div><div class="line">    &#125;,</div><div class="line">    data() &#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">        ...</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="2-将事件控制器作为跟根Vue组件的属性使用"><a href="#2-将事件控制器作为跟根Vue组件的属性使用" class="headerlink" title="2. 将事件控制器作为跟根Vue组件的属性使用"></a>2. 将事件控制器作为跟根Vue组件的属性使用</h3><blockquote>
<p>核心逻辑不变，只是将新建的用于事件派发的<code>vue</code>对象注册到根vue对象中，在其他组件中通过<code>this.$root.eventBus</code>获得事件对象，无需额外引入JS。</p>
</blockquote>
<ol>
<li><p>在<code>main.js</code>中注册<code>EventBus</code>对象</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">new</span> Vue(&#123;</div><div class="line"><span class="attr">el</span>: <span class="string">'#app'</span>,</div><div class="line"><span class="attr">template</span>: <span class="string">'&lt;App/&gt;'</span>,</div><div class="line"><span class="attr">router</span>: router,</div><div class="line"><span class="attr">data</span>: &#123;</div><div class="line">    <span class="attr">eventBus</span>: <span class="keyword">new</span> Vue()</div><div class="line">&#125;,</div><div class="line"><span class="attr">components</span>: &#123; App &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
</li>
<li><p>事件发送</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    data() &#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            ...</div><div class="line">        &#125;</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">methods</span>: &#123;</div><div class="line">        onSelect(item) &#123;</div><div class="line">            <span class="comment">// 发送菜单选中事件</span></div><div class="line">            <span class="keyword">this</span>.$root.eventBus.$emit(<span class="string">'menu-selected'</span>, item)</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>事件捕获</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    created () &#123;</div><div class="line">        <span class="comment">// 监听事件菜单选中事件</span></div><div class="line">        <span class="keyword">this</span>.$root.eventBus.$on(<span class="string">'menu-selected'</span>, item =&gt; &#123;</div><div class="line">            <span class="built_in">console</span>.log(item)</div><div class="line">            ...</div><div class="line">        &#125;)</div><div class="line">    &#125;,</div><div class="line">    data() &#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            ...</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>
<blockquote>
<p>备注: 细心的同学们一定会发现，既然是使用<code>vue</code>对象来实现的，我们何不直接使用<code>this.$root.$emit</code>和<code>this.$root.$on</code>来做呢。其实是完全可以的，实现方法有很多种，只不过个人看来<code>this.$root.eventBus.$emit</code>要比<code>this.$root.$emit</code>表达力更强。</p>
</blockquote>
<h3 id="3-扩展Vue对象属性，一劳永逸"><a href="#3-扩展Vue对象属性，一劳永逸" class="headerlink" title="3. 扩展Vue对象属性，一劳永逸"></a>3. 扩展<code>Vue</code>对象属性，一劳永逸</h3><p>使用<code>prototype</code>为原生Vue添加<code>$eventBus</code>属性即可，那么所有vue组件都可以访问并使用这个属性</p>
<ol>
<li><p>扩展<code>vue</code>对象</p>
<p> 在<code>main.js</code>中</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> vue = <span class="keyword">new</span> Vue(&#123;</div><div class="line">    <span class="attr">el</span>: <span class="string">'#app'</span>,</div><div class="line">    <span class="attr">template</span>: <span class="string">'&lt;App/&gt;'</span>,</div><div class="line">    <span class="attr">router</span>: router,</div><div class="line">    <span class="attr">data</span>: &#123;</div><div class="line">        <span class="attr">eventBus</span>: <span class="keyword">new</span> Vue()</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">components</span>: &#123; App &#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line"><span class="comment">// here</span></div><div class="line">vue.prototype.$eventBus= Vue.prototype.$eventBus || <span class="keyword">new</span> Vue()</div></pre></td></tr></table></figure>
</li>
<li><p>事件发送</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    data() &#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            ...</div><div class="line">        &#125;</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">methods</span>: &#123;</div><div class="line">        onSelect(item) &#123;</div><div class="line">            <span class="comment">// 发送菜单选中事件</span></div><div class="line">            <span class="keyword">this</span>.$eventBus.$emit(<span class="string">'menu-selected'</span>, item)</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>事件捕获</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    created () &#123;</div><div class="line">        <span class="comment">// 监听事件菜单选中事件</span></div><div class="line">        <span class="keyword">this</span>.$eventBus.$on(<span class="string">'menu-selected'</span>, item =&gt; &#123;</div><div class="line">            <span class="built_in">console</span>.log(item)</div><div class="line">            ...</div><div class="line">        &#125;)</div><div class="line">    &#125;,</div><div class="line">    data() &#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            ...</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>
<p>三种实现方案，代码越来越优雅，就其本质来讲，原理是一样的，但知识的深度和广度影响我们实现方法的不同，多写、多练、敢于尝试和不断创新才会进步！</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/vue2/" rel="tag"># vue2</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/技术/CentOS6安装Node-Js/" rel="next" title="CentOS6安装Node.Js">
                <i class="fa fa-chevron-left"></i> CentOS6安装Node.Js
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/工作/某东的技术面试经历/" rel="prev" title="某东的技术面试经历">
                某东的技术面试经历 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="技术/前端/vue2/Vue2组件通信之事件传递/"
           data-title="Vue2组件通信之事件传递" data-url="https://vector-ding.coding.me/技术/前端/vue2/Vue2组件通信之事件传递/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/assets/img/vector.ding.jpg"
               alt="vector.ding" />
          <p class="site-author-name" itemprop="name">vector.ding</p>
          <p class="site-description motion-element" itemprop="description">Elegant Coding</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#背景"><span class="nav-number">1.</span> <span class="nav-text">背景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现"><span class="nav-number">2.</span> <span class="nav-text">实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-将事件控制器抽离出来，作为单独的模块使用"><span class="nav-number">2.1.</span> <span class="nav-text">1. 将事件控制器抽离出来，作为单独的模块使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-将事件控制器作为跟根Vue组件的属性使用"><span class="nav-number">2.2.</span> <span class="nav-text">2. 将事件控制器作为跟根Vue组件的属性使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-扩展Vue对象属性，一劳永逸"><span class="nav-number">2.3.</span> <span class="nav-text">3. 扩展Vue对象属性，一劳永逸</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vector.ding</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"vector-ding"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  












  
  

  

  

  

  

</body>
</html>
